package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingXiShouLi;
import com.vf.admin.miaodong.entity.AppMingxiJinbi;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 账单明细-充值
 */
@Repository
public class AppMingxiJinbiDao extends BaseDao<AppMingxiJinbi> {

    /**
     * 查询列表
     */
    public List<AppMingxiJinbi> findList(Integer userid, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_jinbi where 1=1  ");
        sbStr.append(" and userid=? ");
        paramList.add(userid);
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppMingxiJinbi.class, splitPage);
    }

    public List<AppMingxiJinbi> findList(Map<String, Object> params, Integer userId) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_jinbi where userid = ?");
        paramList.add(userId);

        if (params.containsKey("leixing") && params.get("leixing") != null) {
            sbStr.append(" and leixing = ? ");
            paramList.add(params.get("leixing"));
        }

        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppMingxiJinbi.class);
    }


    /**
     * 详情
     */
    public AppMingxiJinbi get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_jinbi where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppMingxiJinbi.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppMingxiJinbi haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 更新用户头像
     *
     * @param userid
     * @param pic    头像地址
     */
    public void updateImg(Integer userid, String pic) {
        if (userid == null || userid <= 0 || StringEx.isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_mingxi_jinbi SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新用户昵称
     *
     * @param userid
     * @param nick   昵称
     */
    public void updateNick(Integer userid, String nick) {
        if (userid == null || userid <= 0 || StringEx.isNull(nick)) {
            return;
        }
        String sql = " UPDATE app_mingxi_jinbi SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    public List<AppMingxiJinbi> inviteList(Integer userId) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_jinbi where fromType=3 and userid=" + userId);
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppMingxiJinbi.class);
    }

    public List<AppMingxiJinbi> indexCount(Map<String, Object> params) {

        List<Object> paramList = new ArrayList<>();

        StringBuilder str = new StringBuilder();

        str.append("select * from app_mingxi_jinbi where 1=1 ");

        //锁定用户
        if (params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))) {

            //锁定用户
            if (params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))) {
                str.append(" and userid in ( ");
                str.append(params.get("userids"));
                str.append(" ) ");
            }
            // 当天
            if (params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
                str.append(" and createtime like ? ");
                paramList.add("%" + params.get("today") + "%");
            }
            // 全部
            if (params.containsKey("alltoday") && StringEx.isNotNull(params.get("alltoday"))) {
                str.append(" and createtime <= ? ");
                paramList.add(params.get("alltoday"));
            }
            //1 收入 2支出
            if (params.containsKey("leixing") && StringEx.isNotNull(params.get("leixing"))) {
                str.append(" and leixing = ? ");
                paramList.add(params.get("leixing"));
            }
            //条件
            if (params.containsKey("beizhu") && StringEx.isNotNull(params.get("beizhu"))) {
                str.append("and beizhu in (" +"'" + params.get("beizhu") + "'" + ")");
            }

            String sql = str.toString();
            return this.queryList(sql, paramList, AppMingxiJinbi.class);
        }


        return null;
    }


    public List<AppMingxiJinbi> orgInfo(Map<String, Object> params, SplitPageBean splitPage) {
        List<Object> paramList = new ArrayList<>();

        StringBuilder str = new StringBuilder();

        str.append("select * from app_mingxi_jinbi where 1=1 ");
        //锁定用户 及 参数
        if (params.containsKey("userId") && StringEx.isNotNull(params.get("userId"))){
            str.append(" and userid = "+ params.get("userId"));

            str.append(" and beizhu in (" +"'" + params.get("beizhu") + "'" + ")");
        }
        str.append(" and leixing = 1 ");
        String sql = str.toString();
        return this.queryList(sql, paramList, AppMingxiJinbi.class,splitPage);
    }

    public List<AppMingxiJinbi> countNum(Map<String, Object> params) {
        List<Object> paramList = new ArrayList<>();

        StringBuilder str = new StringBuilder();

        str.append("select * from app_mingxi_jinbi where 1=1 ");
        //锁定用户 及 参数
        if (params.containsKey("userId") && StringEx.isNotNull(params.get("userId"))){
            str.append(" and userid = "+ params.get("userId"));

            str.append(" and beizhu in (" +"'" + params.get("beizhu") + "'" + ")");
        }
        str.append(" and leixing = 1 ");
        String sql = str.toString();
        return this.queryList(sql, paramList, AppMingxiJinbi.class);
    }
}
